package com.hsyt.pfc.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hsyt.pfc.redis.serializer.FstRedisSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;

/**
 * @Description: redis自动配置
 * @ClassName: RedisAutoConfiguration
 * @Package: com.hsyt.pfc
 * @Author: wangze@hsyuntai.com
 * @Date: 2018年9月6日 15:10
 * @Copyright: 版权归 HSYUNTAI 所有
 */
@Configuration
public class RedisAutoConfiguration {

//    @Resource
//    private RedisProperties redisProperties;

//    @Bean
//    @ConditionalOnMissingBean(JedisPool.class)
//    public JedisPool jedisPool() {
//        JedisPoolConfig poolConfig = new JedisPoolConfig();
//        poolConfig.setMaxIdle(redisProperties.getPool().getMaxIdle());
//        poolConfig.setMaxTotal(redisProperties.getPool().getMaxActive());
//        poolConfig.setMinIdle(redisProperties.getPool().getMinIdle());
//        poolConfig.setMaxWaitMillis(redisProperties.getPool().getMaxWait());
//        JedisPool jedisPool = new JedisPool(poolConfig, redisProperties.getHost(), redisProperties.getPort(),
//                redisProperties.getTimeout(), redisProperties.getPassword(), redisProperties.getDatabase());
//        return jedisPool;
//    }

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置序列化，默认key为String,value为fastjson
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        Jackson2JsonRedisSerializer jacksonSerializer = new Jackson2JsonRedisSerializer(Object.class);
        jacksonSerializer.setObjectMapper(objectMapper);
        redisTemplate.setDefaultSerializer(jacksonSerializer);
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jacksonSerializer);
        redisTemplate.setValueSerializer(jacksonSerializer);
        return redisTemplate;
    }

    @Bean
    @ConditionalOnMissingBean(name = "fstRedisTemplate")
    public RedisTemplate<Object, Object> fstRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置序列化，默认key为String,value为fastjson
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        FstRedisSerializer fstRedisSerializer = new FstRedisSerializer();
        redisTemplate.setDefaultSerializer(fstRedisSerializer);
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(fstRedisSerializer);
        redisTemplate.setValueSerializer(fstRedisSerializer);
        return redisTemplate;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

//    @Bean
//    public JedisClient jedisClient(JedisConnectionFactory jedisConnectionFactory) {
//        JedisClient jedisClient = new JedisClient(jedisConnectionFactory);
//        return jedisClient;
//    }

    @Bean
    @ConditionalOnMissingBean(RedisSerialNoHandler.class)
    public RedisSerialNoHandler redisSerialNoHandler(RedisTemplate redisTemplate) {
        RedisSerialNoHandler redisSerialNoHandler = new RedisSerialNoHandler(redisTemplate);
        return redisSerialNoHandler;
    }

}
